//创建目录
tmpDir := strconv.FormatInt(time.Now().Unix(), 10)
tmpPath := env.DataDir() + "/help/" + tmpDir + "/"
_, err := os.Stat(tmpPath)
if err != nil {
err = os.MkdirAll(tmpPath, os.ModePerm)
if err != nil {
log.Println(common.GetLogErrorMsg(1001), "\n", err)
return "创建文件夹失败"
}
}
//清空目录
defer os.RemoveAll(tmpPath)
//pdf
pdf := gofpdf.New("P", "mm", "A4", "")
pdf.SetLeftMargin(10.0)
pdf.SetRightMargin(10.0)
pdf.AddPage()
//将字体加载进来
//AddUTF8Font("给字体起个别名", "", "fontPath")
pdf.AddUTF8Font("simfang", "", "library/pdf/PingFangSCRegular.ttf")
//使用这个字体
//SetFont("字体的别名", "", size)
pdf.SetFont("simfang", "", 12)
pdf.WriteAligned(0, 35, title, "C")
pdf.Ln(30)
pdf.WriteAligned(0, 10, content, "L")
pdf.Ln(30)
//download img pic=url数组
for i, img := range pic {
// Get the data
resp, err := http.Get(img)
if err != nil {
panic(err)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
ioutil.WriteFile(tmpPath + strconv.Itoa(i) + ".jpg", data, 0644)
pdf.Image(img, 10, 10, 150, 0, true, "", 0, "")
pdf.Ln(15)
}
//二进制流
//buffer := new(bytes.Buffer)
//if err := pdf.Output(buffer); err != nil {
// panic(err.Error())
//}
if err := pdf.OutputFileAndClose(tmpUploadPath + title + ".pdf"); err != nil {
panic(err.Error())
}
共有 0 - go 生成pdf